using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Diagnostics;
namespace DataPaper
{
    static class DoIt
    {

        public static string Extract(List<Byte> lst_modified, out UInt16 infoNumPage)
        {
            infoNumPage = (UInt16)((lst_modified[1] << 8) + lst_modified[0]);
            UInt16 infoLen = (UInt16)((lst_modified[3] << 8) + lst_modified[2]);
            UInt16 infoCheck = (UInt16)((lst_modified[5] << 8) + lst_modified[4]);
            lst_modified.RemoveRange(0, 6);
            if (lst_modified.Count != infoLen)
            {
                if (lst_modified[infoLen-1] == Convert.ToByte('E'))
                {
                    lst_modified.RemoveRange(infoLen-1, lst_modified.Count - infoLen+1);
                }
                else
                    return "Wrong size.";
            }
            UInt16 check = Check(lst_modified);
            if (infoCheck != check)
                return "Wrong check.";



            return "";
        }

        public static UInt16 Check(List<byte> bytes)
        {
            UInt16 j = 17;
            for (int i = 0; i < bytes.Count; ++i)
                if ((j & 0x8000) == 0x8000)
                    j = (UInt16)(((j << 1) + 1) ^ bytes[i]);
                else
                    j = (UInt16)((j << 1) ^ bytes[i]);
            return j;
        }

        public static int Blocks(Bitmap bmp,
            Point init_top_left,
            double init_u,
            int nbBlock_r, int nbBlock_c, out List<Byte> lst,
            out List<SmallError> smallError,
            out List<LargeError> largeErrors)
        {
            BW bw = new BW(bmp, init_u, init_top_left);
            Border2 bd = new Border2(bw, nbBlock_r, nbBlock_c);
            return Block.Extract(bd, out lst, out smallError, out largeErrors);
        }
    }
}
